/*
 * =====================================================================================
 *
 *       Filename:  str_regex.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  07/04/15 01:03:31
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Andy (gk), andy_y_li@163.com
 *        Company:  RD
 *
 * =====================================================================================
 */

#include "str_regex.h"

PatternErrorCode get_pattern(const char *str, const char *pattern, regmatch_t *pmatch, int len)
{
    regex_t reg;
    char errbuf[1024];
    int err = 0;
    
    if(regcomp(&reg,pattern,REG_EXTENDED) < 0){
        regerror(err,&reg,errbuf,sizeof(errbuf));
        printf("err:%s\n",errbuf);
        return FATAL_ERROR;
    }
    
    err = regexec(&reg,str, len, pmatch,0);
    
    if(err == REG_NOMATCH){
        printf("no match\n");
        return NO_MATCH;
    }else if(err){
        regerror(err,&reg,errbuf,sizeof(errbuf));
        printf("err:%s\n",errbuf);
        return FATAL_ERROR;
    }
    return OK;
}
